﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace XWord.GUI.Views
{
    /// <summary>
    /// Interaction logic for TranslateWordView.xaml
    /// </summary>
    public partial class TranslateWordView : UserControl
    {
        public TranslateWordView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var transArr=originText.Text.Split(Environment.NewLine.ToCharArray());
            Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
            translateText.Text = string.Empty;
            foreach (var item in transArr)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    var baseUrl = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={0}&langpair=";
                    baseUrl = string.Format(baseUrl + "zh-cn|en", item);
                    //if (transType.SelectedIndex == 0)
                    //{
                    //    baseUrl = string.Format(baseUrl + "en|zh-cn", item);
                    //}
                    //else
                    //{
                    //    baseUrl = string.Format(baseUrl + "zh-cn|en", item);
                    //}
                    var transText = WebResourceRequest.CallUrlRequest(baseUrl);
                    var str = js.Deserialize(new StringReader(transText), typeof(TranslateString)) as TranslateString;
                    if (translateText.Text.Length > 0)
                        translateText.Text += Environment.NewLine + str.responseData.translatedText;
                    else
                        translateText.Text += str.responseData.translatedText;
                }
            }
            translateText.Text = translateText.Text.Replace("&quot;", "“");
            
        }
    }

    /// <summary> 
    /// 译文的反系列化类 
    /// </summary> 
    public class TranslateString
    {
        private TranslatedText responsedata;
        public TranslatedText responseData
        {
            get { return responsedata; }
            set { responsedata = value; }
        }
        private string responsedetails;
        public string responseDetails
        {
            get { return responsedetails; }
            set { responsedetails = value; }
        }
        private int responsestatus;
        public int responseStatus
        {
            get { return responsestatus; }
            set { responsestatus = value; }
        }

        /**/
        /// <summary> 
        /// 译文内容 
        /// </summary> 
        public class TranslatedText
        {
            private string translatedtext;
            public string translatedText
            {
                get { return translatedtext; }
                set { translatedtext = value; }
            }
        }
    }
}
